package com.example.community.dto;

import lombok.Data;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

@Data
public class PageDTO {
    private boolean showPre;
    private boolean showFirstPage;
    private boolean showLastPage;
    private boolean showNext;

    private Integer page;
    private Integer totalPage;

    private List<Integer> pages = new ArrayList<>();
    private List<QuestionDTO> questions;

    public void setPagination(Integer totalCount, Integer page, Integer size) {
        this.page =page;

        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        }else {
            totalPage = (totalCount / size ) + 1;
        }

        pages.add(page);
        for (int i = 1;i <= 3;i++){
            if (page - i > 0){
                pages.add(0,page - i);
            }
            if (page + i <= totalPage){
                pages.add(page + i);
            }
        }

        //是否展示左箭头
        if (page == 1){
            showPre = false;
        }else {
            showPre = true;
        }
        //是否展示右箭头
        if (page == totalPage){
            showNext = false;
        }else {
            showNext = true;
        }

        //是否展示跳转到首页
        if (pages.contains(1)){
            showFirstPage = false;
        }else {
            showFirstPage = true;
        }
        //是否展示跳转到末页
        if (pages.contains(totalPage)){
            showLastPage = false;
        }else {
            showLastPage = true;
        }
    }
}
